package com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator;

import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOption;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationType.ConfigurationTypeDouble;
import com.personal.gateway.utils.StringTemplateUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * @author BigHead
 * @date 2022/1/11 17:33
 * @description WebObjectGeneratorInteger
 */
public class WebObjectGeneratorDouble extends AbstractWebObjectGenerator<Double, ConfigurationTypeDouble> {

    // 样例数据

    private static final String EXAMPLE_VALUE = "EXAMPLE_VALUE";
    private static final String EXAMPLE_TARGET_ELEMENT = "placeholder=\"示例： ${EXAMPLE_VALUE}\"";

    // 范围限定参数

    private static final String RANGE_KEY = "RANGE_KEY";
    private static final String MIN_VALUE = "MIN_VALUE";
    private static final String MAX_VALUE = "MAX_VALUE";
    private static final String RANGE_TEMPLATE = "minValue=${MIN_VALUE} maxValue=${MAX_VALUE}";

    // 基础输入组件

    private static final String WEB_ID = "WEB_ID";
    private static final String EXAMPLE_TARGET = "EXAMPLE_TARGET";
    private static final String EXIST_VALUES = "EXIST_VALUES";
    private static final String STRING_INPUT_ELEMENT = "<" +
            "input " +
            "type=\"text\" " +
            "id=\"${WEB_ID}\" " +
            "${EXAMPLE_TARGET} " +
            "autocomplete=\"off\" " +
            "class=\"layui-input\" " +
            "onkeyup=checkDouble(this) \n" +
            "onafterpaste=checkDouble(this) " +
            "${RANGE_KEY} " +
            "${EXIST_VALUES} " +
            ">";

    @Override
    protected String getInputElement(ConfigurationOption<Double, ConfigurationTypeDouble> configurationOption) {
        String exampleTarget = "";
        if (null != configurationOption.getConfigurationType().getExampleValue() && configurationOption.getConfigurationType().getExampleValue().length > 0) {
            exampleTarget += StringTemplateUtils.render(
                    EXAMPLE_TARGET_ELEMENT,
                    new HashMap<String, String>(1) {{
                        put(EXAMPLE_VALUE,
                                Arrays.stream(configurationOption.getConfigurationType().getExampleValue())
                                        .map(String::valueOf)
                                        .collect(Collectors.joining(","))
                        );
                    }}
            );
        }
        String finalExampleTarget = exampleTarget;

        String rangeTemplate = "";
        if (
                null != configurationOption.getConfigurationType().getMinValue() &&
                        null != configurationOption.getConfigurationType().getMaxValue()
        ) {
            rangeTemplate = StringTemplateUtils.render(
                    RANGE_TEMPLATE,
                    new HashMap<String, String>(2) {{
                        put(MIN_VALUE, configurationOption.getConfigurationType().getMinValue().toString());
                        put(MAX_VALUE, configurationOption.getConfigurationType().getMaxValue().toString());
                    }}
            );
        }
        String finalRangeTemple = rangeTemplate;

        return StringTemplateUtils.render(
                STRING_INPUT_ELEMENT,
                new HashMap<String, String>(6) {{
                    put(WEB_ID, webElementIdRecode(configurationOption.getConfigurationKey().getName()));
                    put(EXAMPLE_TARGET, finalExampleTarget);
                    put(ERROR_MARK, null == configurationOption.getConfigurationOptionCheckException() ? "" : ERROR_MARK_CLASS);
                    put(RANGE_KEY, finalRangeTemple);
                    put(EXIST_VALUES, getExistValue(configurationOption));
                }}
        );
    }

    private String getExistValue(ConfigurationOption<Double, ConfigurationTypeDouble> configurationOption) {
        if (null == configurationOption.getConfigurationType().getValue()) {
            return "";
        } else {
            return "value=\"" + configurationOption.getConfigurationType().getValue() + "\"";
        }
    }
}
